<%@page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
    <title>内部投票</title>
	<%@ include file="/common/header/meta.jsp"%>
	<%@ include file="/common/header/script.jsp"%>
	<link rel="stylesheet" href="<%=basePath%>/css/vote.css" />
    <script type="text/javascript">
    var colors = Highcharts.getOptions().colors;
    var options = {
    		chart: {
                type: 'bar'
            },
            title: {
                text: 'Browser market share, April, 2011'
            },
            xAxis: {
            	/* labels: {
            		useHTML: true,
            		formatter: function () {
            			return '<span style="color:#000;">' + this.value + '</span>';
                    }
                }, */
                categories: null,
            },
            yAxis: {
                title: {
                    text: ''
                }
            },
            legend : {
				enabled : false
			},
            plotOptions: {
                column: {
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: colors[0],
                        style: {
                            fontWeight: 'bold'
                        },
                        formatter: function() {
                            return this.y ;
                        }
                    }
                }
            },
            tooltip : {
            	enabled : false,
				formatter : function() {
					return '投票项：<b>' + this.x + '</b><br/>' + '票数: <b>' + this.y + '</b>';
				}
			},
            series: [{
                data: [],
            }],
   	};
    
    
    var chars = [] ;
	$(function() {
		
		var body = $("body") ;
		$.post($.webapp.root+"/admin/system/vote/doNotNeedAuth_viewVote.do", function(result) {
			$.each(result, function(i,p){
				var voteContainer = $("<div>");
				voteContainer.addClass("voteContainer");
				
				var voteDiv = $("<div>");
				voteDiv.attr("id", "voteDiv"+i) ;
				voteDiv.addClass("voteDiv") ;
				
				
				
				var _container = $("<div>") ;
				_container.attr("id", "container"+i) ;
				_container.addClass("_container") ; 
				
				var _subjectDiv = "" ; 
				_subjectDiv += "<div class='subjectDiv'>"+(i+1)+"#"+p.subject+"（"+(p.voteType?"多选":"单选")+"）</div>" ;
				voteContainer.append(_subjectDiv) ;
				
				var datas = [] ;
				var categories = [] ;
				$.each(p.children, function(j, pp) {
					var data = {"y": pp.sum, "color": colors[j], "legendIndex": j} ;
					datas.push(data) ;
					
					var subject = [pp.subject] ;
					categories.push(subject) ;
					
					var aDiv = $("<div>");
					aDiv.addClass("aDiv");
					var str = "";
					//str+="<div class='vote_data'><div class='v1l'>【</div><div class='v1l'>"+pp.sum+"票</div><div class='v1r'>】</div><div class='v1r'>"+pp.percent+"</div></div>" ;
					str+="<div class='vote_data'><div class='v1l'>"+pp.percent+"</div></div>" ;
					str+= "<a href=\"javascript:vote('"+p.id+"','"+pp.id+"',"+p.voteType+","+i+");\">"+pp.subject+"</a>" ;
					//str+= "<div id='icheck' class='v1l'><input type='radio' id='check"+j+"' name='check'><label class='irl' for='check"+j+"'>"+pp.subject+"</label></div>";
					aDiv.html(str) ;
					
					voteDiv.append(aDiv) ;
				});
				//console.info(datas) ;
				
				$('#icheck input').iCheck({
					checkboxClass: 'icheckbox_minimal-blue',
					radioClass: 'iradio_square-blue'
			   	});

				setTimeout(function(){
					options.series[0].data = datas ;
					options.series[0].abc = p.children ;
					options.xAxis.categories = categories ;
					options.title.text = "" ;
					options.chart.renderTo = "container"+i ;
					chars.push(new Highcharts.Chart(options)) ;
					
				}, 100);
				
				voteContainer.append(voteDiv) ;
				voteContainer.append(_container) ;
				
				voteContainer.appendTo(body) ;
			});
		}, 'json').error(function() {alert("发生错误！");});
		
		
	});
	
	function vote(votePid, voteId, voteType, charIndex) {
		$.messager.confirm("您确定要进行该操作？", function (c) { 
			if(c) {
				
				var voteDiv = $("#voteDiv"+charIndex) ;
				
				$.post($.webapp.root+"/admin/system/vote/doNotNeedSession_vote.do", {"pid": votePid, "vid": voteId, "voteType": voteType}, function(result) {
					if(result.status) {
						$.post($.webapp.root+"/admin/system/vote/dotNotNeedSession_getByPid.do", {"id": votePid}, function(result) {
							voteDiv.empty();
							var datas = [] ;
							$.each(result, function(j, pp) {
								var data = {"y": pp.sum, "color": colors[j]} ;
								datas.push(data) ;
								
								var aDiv = $("<div>");
								aDiv.addClass("aDiv");
								
								var str = "";
								str+="<div class='vote_data'><div class='v1l'>"+pp.percent+"</div></div>" ;
								str+= "<a href=\"javascript:vote('"+votePid+"','"+pp.id+"',"+voteType+","+charIndex+");\">"+pp.subject+"</a>" ;
								aDiv.html(str) ;
								
								//var str = "<div class='vote_data'><div class='v1l'>【</div><div class='v1l'>"+pp.sum+"票</div><div class='v1r'>】</div><div class='v1r'>"+pp.percent+"</div></div>" ;
								//str+= "<a href=\"javascript:vote('"+votePid+"','"+pp.id+"',"+voteType+","+charIndex+");\">"+pp.subject+"</a>" ;
								//aDiv.html(str) ;
								
								voteDiv.append(aDiv);
							});
							
							
							chars[charIndex].series[0].remove(false);
							chars[charIndex].addSeries({
								name: 'Browser brands',
								data: datas,
								color: 'white'
							}, false);
							chars[charIndex].redraw();
							
							alertify.success("投票成功！！！"); return ;
						}, 'json').error(function() {alert("发生错误！");});
					} else {
						alertify.warning("以投过票了！！！"); return ;
					}
		 			
				}, 'json').error(function() {alert("发生错误！");});
			}
		});
		
	}
	
	
    </script>
</head>

<body>
</body>
</html>



